package game;

import java.util.HashMap;
import java.util.SortedSet;
import java.util.TreeSet;

/**
 * Scoreboard, this class will keep track of all the players' scores.
 * @author Twisty
 *
 */
public class Scoreboard {
	// A map with user/score combinations.
	private static HashMap<String, Integer> scores = new HashMap<String, Integer>();
	
	/**
	 * Get the score for a given player.
	 * @param playerName	The player to get the score for.
	 * @return The associated score.
	 */
	public static Integer getScore(String playerName){
		return scores.get(playerName);
	}
	
	/**
	 * Increase the score for a given player.
	 * @param playerName	The player to increase the score for.
	 */
	public static void increaseScore(String playerName){
		if(scores.get(playerName) == null){
			scores.put(playerName, 1);
		}else{
			scores.put(playerName, scores.get(playerName)+1);
		}
	}
	
	/**
	 * Get the name of the top player.
	 * @return The name of the top player.
	 */
	public static String topPlayer(){
		int highest = 0;
		for(String key : scores.keySet()){
			if(scores.get(key) > highest){
				highest = scores.get(key);
			}
		}
		for(String key : scores.keySet()){
			if(scores.get(key) == highest){
				return key;
			}
		}
		return "";
	}
	
	/**
	 * Remove a player from the score board.
	 * @param playerName	The player to remove.
	 */
	public static void removeScore(String playerName){
		scores.remove(playerName);
	}
	
	/**
	 * Reset all scores.
	 */
	public static void resetAllScores(){
		for(String key: scores.keySet()){
			scores.put(key, 0);
		}
	}
	
	/**
	 * Get all scores concatenated as a string.
	 * @return The scores as a concatenated string.
	 */
	public static String getScoresAsString(){
		String result = "Scoreboard \n";
		SortedSet<String> keys = new TreeSet<String>(scores.keySet());
		for (String key : keys) {
			result += (key + ":\t" + scores.get(key) + "\n");
		}
		return result;
	}
	
	/**
	 * Get all the scores.
	 * @return All the scores as a map.
	 */
	public static HashMap<String, Integer> getScores(){
		return scores;
	}
	
	/**
	 * Add a player to the score board.
	 * @param playerName 	The player to add.
	 */
	public static void addPlayer(String playerName){
		if(!playerName.equals("null")){
			scores.put(playerName, 0);
		}
	}
}
